package com.example.hehecat.yuejian1.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.hehecat.yuejian1.R;
import com.example.hehecat.yuejian1.mvp.contract.PictureListContract;
import com.example.hehecat.yuejian1.mvp.model.bean.PictureBean;
import com.example.hehecat.yuejian1.mvp.presenter.PictureListPresenter;
import com.example.hehecat.yuejian1.ui.activity.IssuePictureActivity;
import com.example.hehecat.yuejian1.ui.adapter.PictureListAdapter;
import com.example.hehecat.yuejian1.ui.wiget.MyItemDecoration;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.content.ContentValues.TAG;


public class PictureFragment extends Fragment implements PictureListContract.View {
    Unbinder unbinder;
    @BindView(R.id.picture_list)
    RecyclerView pictureList;
    @BindView(R.id.TextView)
    TextView TextView;
    @BindView(R.id.issue)
    ImageView issue;


    private PictureListAdapter adapter;
    private PictureListPresenter presenter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_picture, container, false);//若为true，则第二个参数意义是：从第一个参数填充成的View对象的父控件。若为fales，第二个参数是可以为第一个参数生成的view对象的根布局提供一系列LayoutParams参数的控件
        unbinder = ButterKnife.bind(this, view);


        //创建布局管理
        final RecyclerView recyclerView = view.findViewById(R.id.picture_list);
        /*RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL,false);*/
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new MyItemDecoration());

        //创建适配器
        adapter = new PictureListAdapter(R.layout.item_list, getContext());
        //监听item点击
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter madapter, View view, int position) {
                moveToPictureInfo(Objects.requireNonNull(adapter.getItem(position)).getPictureid());
                Log.d(TAG, "onItemClick: " + adapter.getItem(position).getPictureid());
            }
        });
        //给RecyclerView设置适配器
        presenter = new PictureListPresenter(this);
        presenter.updateAdapterData();
        recyclerView.setAdapter(adapter);
        return view;


    }


//    public static PictureFragment newInstance(String name) {
//        PictureFragment fragment = new PictureFragment();
//        Bundle args = new Bundle();
//        args.putString("name", name);
//        fragment.setArguments(args);
//        return fragment;
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void updateAdapterData(List<PictureBean> data) {
        adapter.addData(data);
    }

    @Override
    public void moveToPictureInfo(int picture_id) {
        PictureInfoFragment pictureInfoFragment = PictureInfoFragment.newInstance(picture_id);
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.id_content, pictureInfoFragment, null)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .addToBackStack(null)
                .commit();
    }


    @OnClick(R.id.issue)
    void btIssueOnClick() {
        startActivity(new Intent(getActivity(), IssuePictureActivity.class));
    }

}